package com.fzc.rhinooffice.module.office;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.JsonReader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.fzc.rhinooffice.R;
import com.fzc.rhinooffice.common.RemoteInvoke;
import com.fzc.rhinooffice.common.utils.DateUtil;
import com.fzc.rhinooffice.common.utils.JsonUtil;
import com.fzc.rhinooffice.common.utils.StringUtil;
import com.fzc.rhinooffice.common.view.PullRefreshListView;
import com.fzc.rhinooffice.module.BaseActivity;
import com.fzc.rhinooffice.module.CommonAdapter;
import com.fzc.rhinooffice.module.ViewHolder;
import com.fzc.rhinooffice.module.entity.Sms;
import com.fzc.rhinooffice.module.workbench.SmsDetailActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.StringReader;
import java.util.List;

/**
 * Created by cong on 15/6/9.
 */
@ContentView(R.layout.activity_sms_list)
public class SmsListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @ViewInject(R.id.tv_title)
    private TextView tv_title;

    @ViewInject(R.id.list_sms)
    public PullRefreshListView list_sms;

    @ViewInject(R.id.srl_refresh_view)
    private SwipeRefreshLayout srl_refresh_view;

    private int page = 1;
    private CommonAdapter<Sms> commonAdapter;

    private List<Sms> smsList;

    private Intent mIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initUI();
    }

    private void initUI() {
        tv_title.setText("短信");
        list_sms.setOnItemClickListener(this);

        srl_refresh_view.setColorSchemeResources(R.color.swipe_color_1, R.color.swipe_color_2, R.color.swipe_color_3, R.color.swipe_color_4);
        srl_refresh_view.setSize(SwipeRefreshLayout.LARGE);
        srl_refresh_view.setProgressViewEndTarget(true, 100);

        //加载更多的监听
        list_sms.setPullLoadMoreListener(new PullRefreshListView.PullLoadMoreListener() {
            @Override
            public void loadMore() {//加载更多
                page++;
                getSmss();
            }
        });
        //下拉刷新还是官方的那个
        srl_refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getSmss();
            }
        });
        //自动下拉，模拟用户手势实现
        list_sms.startAutoRefresh(srl_refresh_view);//自动下拉

    }

    private void getSmss() {


        RemoteInvoke.sms(mHandler, "2010-04-30", DateUtil.getDataByY_M_D(), "", page);
    }

    @OnClick({R.id.iv_back, R.id.tv_title})
    private void OpenDL(View v) {
        super.back();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        mIntent = new Intent(this, SmsDetailActivity.class);

        Bundle mBundle = new Bundle();
        mBundle.putSerializable("sms", smsList.get(position));
        mIntent.putExtras(mBundle);

//        //mIntent.putExtra("email_id", "16");	//测试
        startActivity(mIntent);
    }


    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            srl_refresh_view.setRefreshing(false);
            //加载完毕告诉listView结束了
            list_sms.endRefresh();//结束加载
            switch (msg.what) {


                case 1: {
                    JSONObject jsonObject = (JSONObject) msg.obj;
                    if (jsonObject != null) {
                        try {
                            if (jsonObject.getString("record") != null
                                    && !"".equals(jsonObject.getString("record"))) {

                                if (smsList == null || page == 1) {
                                    smsList = JsonUtil.analysis_smss(new JsonReader(new StringReader(jsonObject.optString("record"))));
                                } else {
                                    smsList.addAll(JsonUtil.analysis_smss(new JsonReader(new StringReader(jsonObject.optString("record")))));
                                }

                                if (smsList != null && smsList.size() != 0) {
                                    initAdapter();
                                } else {
                                    StringUtil.showToast("暂无可读短信");
                                }
                            } else {
                                StringUtil.showToast("暂无可读短信");

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            StringUtil.showToast("未知错误");
                        }
                    } else {
                        StringUtil.showToast("未知错误");

                    }
                }
                break;

                case -1:
                    StringUtil.showToast("加载失败！");
                    break;
            }
        }


    };


    private void initAdapter() {
        LogUtils.e("刷新短信");
        if (commonAdapter == null) {
            commonAdapter = new CommonAdapter<Sms>(this, smsList, R.layout.sms_list_item) {

                @Override
                public void convert(ViewHolder holder, Sms item) {

                    holder.setText(R.id.tv_from_name, item.from_name);
                    holder.setText(R.id.tv_time, item.send_time.replace(" ","\n"));
                    holder.setText(R.id.tv_content, item.content + "\n…");
                    holder.setText(R.id.tv_sms_detail_avatar,item.from_name.substring(0,1));

//                    if (item.is_new.equals("0")) {
//                        holder.setViewVisibility(R.id.iv_isnew, View.VISIBLE);
//                    } else {
//                        holder.setViewVisibility(R.id.iv_isnew, View.INVISIBLE);
//                    }

                }
            };
            list_sms.setAdapter(commonAdapter);

        } else {
            commonAdapter.notifyDataSetChanged();
        }
    }

}